﻿<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>角色权限分配</title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="shortcut icon" href="../../../favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="../../../static/css/font.css">
    <link rel="stylesheet" href="../../../static/css/weadmin.css">
    <script type="text/javascript" src="../../../lib/layui/layui.js" charset="utf-8"></script>
</head>
<body>
    <div class="weadmin-nav">
        <span class="layui-breadcrumb" style="visibility: visible;">
            <a href="#">首页</a><span lay-separator="">/</span>
            <a href="#">基础数据设置</a><span lay-separator="">/</span>
            <a><cite>角色权限分配</cite></a>
        </span>
        <a class="layui-btn layui-btn-sm" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon" style="line-height:30px">ဂ</i>
        </a>
    </div>
    <div class="left-nav" style="top:46px; bottom:0px; padding-top: 0px">
        <div id="side-nav">
            <div class="layui-form">
                <ul id="nav"></ul>
            </div>
        </div>
    </div>
    <div class="page-content" style="top:0px; bottom:0px;">
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">
                <table class="layui-hide" id="memberList" lay-filter="memberList"></table>
            </div>
        </div>
    </div>
    <script id="toolbarDemo" type="text/html">
        <button class="layui-btn layui-btn-sm" lay-event="saveRoleSet" lay-submit="">确定保存</button>
    </script>
    <script>
        layui.use(['table', 'jquery', 'layer', 'form'], function () {
            var table = layui.table,
                $ = layui.jquery,
                layer = layui.layer;
            form = layui.form;
            $.support.cors = true;
            $.ajaxSetup({
                cache: false,
                headers: {
                    "userid": JSON.parse(window.localStorage.authInfo).UserId,
                    "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                },
            })
            $(document).css("height", "200px");
            var roleid;
            table = $.extend(table, { config: { checkName: 'checkbox' } });
            //获取页面角色id，并根据角色id获取对应模块，并进行对应的遍历绑定
            $('ul').on('click', 'li', function () {
                roleid = $(this).attr('id');//获取到绑定的li的id即roleid
                $.each($("#nav li"), function () {
                    $(this).css("background", "RGB(238,238,238)");
                })
                $(this).css("background", "white");
                var postdata = { "RoleID": roleid };
                //post请求api得到该角色所拥有模块并进行对应绑定
                $.post(ApiUrl + 'RoleSet/GetModuleByRoleID', postdata, function (data) {
                    if (data.StatusCode == 200) {
                        //赋值给界面列表
                        $(function () { showList(data.Data); })
                    } else {
                        layer.msg(data.Info);
                    }
                });
            });
            //查询遍历角色信息
            $.ajax({
                url: ApiUrl + "Role/Sel",
                type: 'POST',
                headers: {
                    "userid": JSON.parse(window.localStorage.getItem("authInfo")).UserId,
                    "signtoken": JSON.parse(window.localStorage.getItem("authInfo")).SignToken,
                },
                success: function (data) {
                    if (data.StatusCode == 200) {
                        $(data.Data).each(function (index, item) {
                            var htmlmenu = "";
                            htmlmenu += '<li id="' + item.RoleID + '">';
                            htmlmenu += '<a href="javascript:;"><i class="iconfont">&#xe6b8;</i><cite>' + item.RoleName + '</cite></a>';
                            htmlmenu += "</li>";
                            $("#nav").append(htmlmenu);
                        });
                    }
                    else if (data.StatusCode == 403) {
                        top.layer.alert(data.Info + ",请重新登录！", { closeBtn: 0 }, function () {
                            top.location.href = "http://" + top.location.host + "/login.html";
                        })
                    }
                    else {
                        layer.msg(data.Info);
                    }
                }
            });
            //查询所有模块信息
            $(function () {
                var postdata = { "swhere": "", "startindex": "1", "endindex": "100" }
                $.post(ApiUrl + 'Module/Sel', postdata, function (data) {
                    if (data == null)
                        return '提交错误'
                    if (data.StatusCode == 200) {
                        showList(data.Data);
                    } else {
                        return data.Info;
                    }
                });
            })
            function showList(data) {
                table.render({
                    elem: '#memberList',
                    toolbar: '#toolbarDemo',
                    cellMinWidth: 80,
                    height: 'full-45', //高度最大化减去差值,
                    cols: [[
                        {
                            type: 'checkbox', field: 'checkbox'
                        }, {
                            field: 'ModID', title: '模块编号', sort: true
                        }, {
                            field: 'ModName', title: '模块名字'
                        }, {
                            field: 'ModDesc', title: '模块描述'
                        }, {
                            field: 'ModURL', title: '模块地址'
                        }, {
                            field: 'ModParentID', title: '模块父级ID'
                        }, {
                            field: 'ModLevel', title: '模块级别'
                        },
                    ]],
                    data: data,
                    event: true,
                    limit: 50,
                });
            }

            //监听头工具栏事件
            table.on('toolbar(memberList)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'saveRoleSet'://保存角色所拥有的权限
                        var data = checkStatus.data;
                        //如果获取到模块
                        if (data.length > 0) {
                            var modids = "";
                            //遍历得到modid
                            $(data).each(function (index, item) {
                                modids += item.ModID + ",";
                            });
                            //处理最后一个逗号
                            if (modids.length > 0) { modids = modids.substring(0, modids.length - 1); }
                            //如果已经选择角色，那么通过api提交保存角色所拥有的权限
                            if (roleid > 0) {
                                var postdata = { "RoleID": roleid, "ModID": modids };
                                $.post(ApiUrl + 'RoleSet/Add', postdata, function (data) {
                                    if (data.StatusCode == 200) {
                                        layer.msg(data.Info);
                                    } else {
                                        layer.msg(data.Info);
                                    }
                                });
                            } else {
                                layer.msg("请先选择左边的角色");
                            }
                        } else { layer.msg("请选择要分配的权限"); }
                        break;
                };
            });
        });
    </script>
</body>
</html>
